using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace WindowsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            Uri location = new Uri("http://people.icq.com/people/about_me.php?uin=" + textBox1.Text);
            textBox2.Text = GetNickByNumber(location);
        }

        private string GetNickByNumber(Uri location) {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(location);
            byte[] encodedPostData = new byte[0];
            webRequest.Method = "GET";
            webRequest.ContentType = "text/html";
            webRequest.Headers["Accept-Language"] = "en-us,en;q=0.5";
            webRequest.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
            webRequest.ProtocolVersion = HttpVersion.Version11;
            webRequest.AllowAutoRedirect = false;
            webRequest.KeepAlive = true;
            webRequest.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3";
            webRequest.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";

            // Attempt to get response from server
            HttpWebResponse webResponse = null;
            string output = "";
            try {
                // get response
                webResponse = (HttpWebResponse)webRequest.GetResponse();
                string headers = "";
                foreach (string key in webResponse.Headers.Keys) {
                    headers += key + "=" + webResponse.Headers[key] + " | ";
                }
                // read response stream and dump to string
                Stream streamResponse = webResponse.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);

                output = streamRead.ReadToEnd();

                output = GetTitle(output);

                streamRead.Close();
                streamResponse.Close();

            } catch (Exception ex) {
            } finally {
                if (webResponse != null) {
                    webResponse.Close();
                }
            }

            return output;
        }
        private string GetTitle(string html) {
            Regex reTitle = new Regex(@"<title\b[^>]*>(.*?)</title", RegexOptions.IgnoreCase & RegexOptions.Multiline);
            string title = "";
            if (reTitle.Matches(html).Count > 0) {
                title = reTitle.Matches(html)[0].Groups[1].Value;
                title = title.Substring(0, title.IndexOf(" - ICQ.com"));
            }
            return title;
        }

        private void timer1_Tick(object sender, EventArgs e) {
            if (progressBar1.Value == progressBar1.Maximum)
                progressBar1.Value = progressBar1.Minimum;
            progressBar1.PerformStep();
        }
    }
}